package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.enums.OrderStatusEnum;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.domain.OrderUserCount;
import com.ruoyi.system.mapper.OrderUserCountMapper;
import com.ruoyi.system.service.BaseServiceImpl;
import com.ruoyi.system.service.OrderService;
import com.ruoyi.system.service.OrderUserCountService;
import com.ruoyi.system.vo.OrderAgentCountVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;


@Service
@Slf4j
public class OrderUserCountServiceImpl extends BaseServiceImpl<OrderUserCountMapper, OrderUserCount> implements OrderUserCountService {

    @Resource
    private OrderService orderService;

    public OrderAgentCountVo count(Order order, Date start, Date end) {
        OrderAgentCountVo orderAgentCountVo = new OrderAgentCountVo();
        PageDomain pageDomain = new PageDomain();
        pageDomain.setBeginTime(DateUtil.formatDateTime(DateUtil.beginOfDay(new Date())));
        pageDomain.setEndTime(DateUtil.now());
        //总单数
        int totalNum = orderService.countByDomain(order, start, end);
        //总金额
        BigDecimal todayAmount = orderService.sum("amount", order, start, end);
        order.setOrderStatus(OrderStatusEnum.success.getValue());
        //成功单数
        int finishNum = orderService.countByDomain(order, start, end);
        //成功金额
        BigDecimal finishAmount = orderService.sum("amount", order, start, end);
        orderAgentCountVo.setTotalAmount(todayAmount);
        orderAgentCountVo.setTotalNum(totalNum);
        orderAgentCountVo.setFinishNum(finishNum);
        orderAgentCountVo.setFinishAmount(finishAmount);

        return orderAgentCountVo;
    }


}
